package com.jhgsys.internal.common.controller;

import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.system.entity.Role;
import com.jhgsys.internal.system.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@Slf4j
@Validated
@RestController
public class BasicRoleCommonController extends BaseController {

    @Autowired
    private IRoleService roleService;


    @GetMapping("common/role/list")
    public FebsResponse getRoleList(Role role, QueryRequest request) throws FebsException {
        try {
            //总公司看全部分公司只能查对应公司角色
            if (StringUtils.isBlank(role.getTenantId())) {
                role.setTenantId(getCurrentTenantId());
            }
            Map<String, Object> dataTable = getDataTable(this.roleService.findRoles(role, request));
            return new FebsResponse().success().data(dataTable);
        } catch (Exception e) {
            String message = "获取角色失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
